﻿using Microsoft.EntityFrameworkCore;
using SheepStorage.Domain;
using SheepStorage.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Repository.BaseImp
{
    //基础类
    public class BaseRepository<T>: IBaseRepository<T> where T : class
    {
        private readonly SheepDbContext _db;

        public BaseRepository(SheepDbContext db)
        {
            _db = db;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return _db.Set<T>().AsQueryable();
             
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add(T entity)
        {
            _db.Set<T>().Add(entity);
            return _db.SaveChanges();
        }
        /// <summary>
        /// 批量添加信息
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        public int AddRange(List<T> entitys)
        {
            _db.Set<T>().AddRange(entitys);
            return _db.SaveChanges();
        }
        //修改-逻辑删除
        public int Update(T entity)
        {
            _db.Set<T>().Update(entity);
            return _db.SaveChanges();
        }

        //删除物理删除
        public int Delete(T entity)
        {
            _db.Set<T>().Remove(entity);
            return _db.SaveChanges();
        }
        //批量删除-物理删除
        public int DeleteRange(List<T> entity)
        {
            _db.Set<T>().RemoveRange(entity);
            return _db.SaveChanges();
        }

        // 批量删除/逻辑删除
        public int UpdateRange(List<T> idList)
        {
            _db.Set<T>().UpdateRange(idList);
        
            return _db.SaveChanges();
        }

      

        //查询单个
        public T GetInfoBy(int id)
        {
            return _db.Set<T>().Find(id);
        }



        /// <summary>
        /// 单个删除 真
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Del(int id)
        {
           var list= _db.Set<T>().Find(id);
            _db.Set<T>().Remove(list);  
            return _db.SaveChanges();   
        }

      

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetValue(int id)
        {
            return _db.Set<T>().Find(id);
        }

    }
}
